home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / B-C / C++Source Code Fmtr.sit / Src / Vector.cp / Vector.cp
Encoding:
Text File  |  1992-04-28  |  1.9 KB  |  136 lines  |  [TEXT/MPS ]

  1. #ifndef __VECTOR__
  2. #include "Vector.h"
  3. #endif
  4.  
  5. #ifndef __ERRORS__
  6. #include <errors.h>
  7. #endif
  8.  
  9. #ifndef __STDLIB__
  10. #include <stdlib.h>
  11. #endif
  12.  
  13.  
  14.  
  15. //µ   Vector::Vector
  16. #pragma segment Vector
  17. Vector::Vector()
  18.     : DataArea(),
  19.       fLength(0)
  20.     {
  21.     }
  22.  
  23.  
  24.  
  25. //µ   Vector::IVector
  26. #pragma segment Vector
  27. int Vector::IVector(size_t initialSize, size_t increment)
  28. {
  29.     fLength = 0;
  30.     return (IDataArea(initialSize * sizeof(void *), increment * sizeof(void *)));
  31. }
  32.  
  33.  
  34.  
  35.  
  36. //µ   Vector::IVector
  37. #pragma segment Vector
  38. int Vector::IVector(const Vector *aVector)
  39. {
  40.     if (aVector == 0)
  41.         return (memPCErr);
  42.     fLength = aVector->fLength;
  43.     return (IDataArea(aVector));
  44. }
  45.  
  46.  
  47.  
  48.  
  49. //µ   Vector::AtPut
  50. #pragma segment Vector
  51. void Vector::AtPut(int i, const void *anItem)
  52. {
  53.     if (i >= 0 && i < fLength)
  54.         _AtPut(i, anItem);
  55. }
  56.  
  57.  
  58.  
  59.  
  60. //µ   Vector::IndexOf
  61. #pragma segment Vector
  62. int Vector::IndexOf(const void *anItem)
  63. {
  64.     int n = fLength;
  65.     void **p = (void **)GetData(0);
  66.  
  67.     for (; n; ++p, --n)
  68.         if (*p == anItem)
  69.             return (fLength - n);
  70.  
  71.     return (-1);
  72. }
  73.  
  74.  
  75.  
  76.  
  77. //µ   Vector::AddOnce
  78. #pragma segment Vector
  79. Boolean Vector::AddOnce(const void *anItem)
  80. {
  81.     return ((IndexOf(anItem) >= 0) ? true : AddItem(anItem));
  82. }
  83.  
  84.  
  85.  
  86.  
  87. //µ   Vector::AddOnce
  88. #pragma segment Vector
  89. Boolean Vector::AddOnce(const Vector *aVector)
  90. {
  91.     for (int i = 0; i < aVector->Length(); i++)
  92.         if (!AddOnce(aVector->At(i)))
  93.             return (false);
  94.  
  95.     return (true);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. //µ   Vector::AddItem
  102. #pragma segment Vector
  103. Boolean Vector::AddItem(const void *anItem)
  104. {
  105.     if (Require(sizeof(anItem)) >= sizeof(anItem)) {
  106.         _AtPut(fLength++, anItem);
  107.         IncrCursor(sizeof(anItem));
  108.         return (true);
  109.     }
  110.  
  111.     return (false);
  112. }
  113.  
  114.  
  115.  
  116. //µ   Vector::Sort
  117. #pragma segment Vector
  118. void Vector::Sort(int (*aCompareFunc)(const void *, const void *))
  119. {
  120.     HLock();
  121.     qsort(*GetHandle(), Length(), sizeof(void*), aCompareFunc);
  122.     HUnlock();
  123. }
  124.  
  125.  
  126.  
  127.  
  128. //µ   Vector::MakeEmpty
  129. #pragma segment Vector
  130. void Vector::MakeEmpty()
  131. {
  132.     fLength = 0;
  133.     SetCursor(0);
  134.     Truncate();
  135. }
  136.